import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/routes/app_routes.dart';
import 'package:seedfour/app/views/module_seal/seal_create/logic.dart';

import '../../../constants.dart';
import '../../../models/seal_list_entity.dart';
import '../../../services/api_service.dart';
import '../../../services/common_service.dart';

class SealLogic extends GetxController with GetSingleTickerProviderStateMixin {
  final apiService = Get.find<ApiService>();
  final commonService = Get.find<CommonService>();
  final loadingTip = '加载中...';
  final noMoreTip = '没有更多了';

  var tabTitles = ['待审批', '处理中', '已完成'].obs;
  var isLoading = true.obs;
  var isSearching = false.obs;
  var isSearched = false.obs;
  var bottomTips = ''.obs;
  var sealList = <SealInfo>[].obs;
  var currentPaperType = SealPaperType.all.obs;
  var createPaperType = Rxn<SealPaperType>();

  var isReloadList = false.obs;

  late TabController tabController;
  late ScrollController scrollController;
  late TextEditingController searchController;

  final pageSize = 20;
  final statusParams = [1, 2, 3];

  int _page = 1;
  int _status = 0;

  @override
  void onInit() {
    super.onInit();

    tabController = TabController(length: tabTitles.length, vsync: this);
    tabController.addListener(() {
      if (tabController.index.toDouble() == tabController.animation?.value) {
        _status = tabController.index;
        reload();
      }
    });

    scrollController = ScrollController();
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        bottomTips.value = loadingTip;
        _listSeal(_page, _status, currentPaperType.value.value);
      }
    });

    searchController = TextEditingController();

    reload();
  }

  void onClickSearch() {
    isSearching.value = true;
    isSearched.value = false;
  }

  void onCancelSearch() {
    isSearching.value = false;
    isSearched.value = false;
    searchController.clear();
  }

  void onSearch() {
    if (searchController.text.isNotEmpty) {
      _page = 1;
      sealList.clear();
      isLoading.value = true;
      _listSeal(_page, _status, currentPaperType.value.value);
    }
  }

  void onChangeType(SealPaperType type) {
    currentPaperType.value = type;
    reload(listLoading: true);
  }

  void onSelectedCreateType(SealPaperType type) {
    createPaperType.value = type;
  }

  void toCreateSealApply() async {
    dynamic result;
    result = await Get.toNamed(RouteGet.sealCreate,
        arguments: SealCreateParams(sealPaperType: createPaperType.value!));
    if (result != null && result as bool == true) {
      reload();
    }
  }

  void toSealDetail(int index) {
    Get.toNamed(RouteGet.sealDetail, arguments: sealList[index].id);
  }

  void reload({bool listLoading = false}) {
    if (listLoading) {
      isReloadList.value = true;
    } else {
      isLoading.value = true;
    }
    _page = 1;
    isSearched.value = false;
    isSearching.value = false;
    sealList.clear();
    _listSeal(_page, _status, currentPaperType.value.value);
  }

  void _listSeal(int page, int status, int type) {
    apiService
        .listSeal(
          page,
          pageSize,
          commonService.getRole(),
          statusParams[status],
          type,
          keyword: isSearching.value ? searchController.text : '',
        )
        .then((value) => {
              isLoading.value = false,
              isSearched.value = true,
              isReloadList.value = false,
              _page++,
              if (value != null)
                {
                  sealList.addAll(value.data!),
                  if (value.data?.length != pageSize)
                    {bottomTips.value = noMoreTip}
                }
              else
                {bottomTips.value = noMoreTip}
            });
  }
}
